﻿/**
 * Created by Administrator on 2017/8/18 0018.
 */
var express = require('express');
var crypto = require("crypto");
var qr = require('qr-image')
var multer = require("multer");
var config = require('../../config.js');
var db = require('../../db/db.js');
var moment = require("moment");

var router = express.Router();
var user_model = require('../../model/yoshop_user.js');

var yoshop_user_address_model = require('../../model/yoshop_user_address.js');



router.post('/list', async function (req, res) {
    var page;
    var rows;
    if (isNaN(req.body.page)) {
        page = 1;
    } else {
        page = +req.body.page;
    }
    if (isNaN(req.body.rows)) {
        rows = 10;
    } else {
        rows = +req.body.rows;
    }
    var first = rows * (page - 1);

    var where = " 1=1 ";
    if (typeof (req.body.phone) != "undefined" && req.body.phone != "") {
        where = where + " and yoshop_user.phone like  '%" + req.body.phone + "%'";

    }

    if (req.body.mid != undefined && req.body.mid != "" && req.body.mid != "0") {
        where += "  and yoshop_user.wxapp_id=" + req.body.mid;

    }


    console.log("where", where);
    var list = await user_model.getlist(rows, first, where);
    for (var i = 0; i < list.length; i++) {

        list[i].create_time = moment(list[i].create_time).format("YYYY-MM-DD HH:mm:ss");


        // var address = await yoshop_user_address_model.getinfo("user_id=" + list[i].user_id);
        // if (address.length > 0) {
        //     list[i].idcardimg1 = address[0].idcardimg1;
        //     list[i].idcardimg2 = address[0].idcardimg2;
        //     list[i].idcard = address[0].idcard;
        // }


    }
    var count = await user_model.getcount(where);
    var allcount = parseInt(parseInt(count) / rows) + 1;
    if (count % rows == 0) {
        allcount = allcount - 1;
    }

    res.send({ status: 200, data: list, message: "", allcount: allcount, count: count });

});


router.delete('/del', async function (req, res) {

    var where = " id=" + req.body.id;
    var obj = await goods_model.del(where);
    if (obj.code == 200) {

        var result = {
            "status": "200",
            "message": "success"
        }
        res.jsonp(result);
    } else {
        var result = {
            "status": "400",
            "message": "success"
        }
        res.jsonp(result);
    }

});

router.post('/config', async function (req, res) {

    var user = await user_model.getinfo("phone='" + req.body.phone + "'");
    if (user.length == 0) {
        var result = {
            "status": "400",
            "message": "用户名不存在"
        }
        res.jsonp(result);
        return;
    }

    var obj = await db.execute("update yoshop_user set config=? where phone=?", [req.body.config, req.body.phone]);
    console.log("obj", obj);

    if (obj.code == 200) {

        var result = {
            "status": "200",
            "message": "success"
        }
        res.jsonp(result);
    } else {
        var result = {
            "status": "400",
            "message": "err"
        }
        res.jsonp(result);
    }

});

router.post('/info', async function (req, res) {

    var user = await user_model.getinfo("phone='" + req.body.phone + "'");
    if (user.length == 0) {
        var result = {
            "status": "400",
            "message": "用户名不存在"
        }
        res.jsonp(result);

    } else {
        var result = {
            "status": "200",
            "message": "用户名不存在",
            data: user[0]
        }
        res.jsonp(result);

    }



});




router.post('/address', async function (req, res) {
    var page;
    var rows;
    if (isNaN(req.body.page)) {
        page = 1;
    } else {
        page = +req.body.page;
    }
    if (isNaN(req.body.rows)) {
        rows = 10;
    } else {
        rows = +req.body.rows;
    }
    var first = rows * (page - 1);

    var where = " 1=1 ";
    if (typeof (req.body.user_id) != "undefined" && req.body.user_id != "") {
        where = where + " and yoshop_user_address.user_id=" + req.body.user_id;

    }



    console.log("where", where);
    var list = await yoshop_user_address_model.getlist(rows, first, where);
    for (var i = 0; i < list.length; i++) {

        if (list[i].create_time != null && list[i].create_time != "") {
            list[i].create_time = moment(list[i].create_time).format("YYYY-MM-DD HH:mm:ss");

        }

    }
    var count = await yoshop_user_address_model.getcount(where);
    var allcount = parseInt(parseInt(count) / rows) + 1;
    if (count % rows == 0) {
        allcount = allcount - 1;
    }

    res.send({ status: 200, data: list, message: "", allcount: allcount, count: count });

});




router.post('/address/add', async function (req, res) {

    var data = req.body;
    data.create_time = new Date();
    var obj = await yoshop_user_address_model.add(data);
    if (obj.code == 200) {


        var result = {
            "status": "200",
            "message": "success"
        }
        res.jsonp(result);
    } else {
        var result = {
            "status": "400",
            "message": "error"
        }
        res.jsonp(result);
    }


})


router.post("/address/edit", async function (req, res) {

    var data = req.body;
    if (data.ischeck == '') {
        data.ischeck = 0;
    }
    var obj = await yoshop_user_address_model.edit(data);
    if (obj.code == 200) {

        var result = {
            "status": "200",
            "message": "success"
        }
        res.jsonp(result);
    } else {
        var result = {
            "status": "400",
            "message": "error"
        }
        res.jsonp(result);
    }

})

router.delete('/address/del', async function (req, res) {

    var where = " id in (" + req.body.ids + ")";
    var obj = await yoshop_user_address_model.del(where);
    if (obj.code == 200) {

        var result = {
            "status": "200",
            "message": "success"
        }
        res.jsonp(result);
    } else {
        var result = {
            "status": "400",
            "message": "success"
        }
        res.jsonp(result);
    }

});
module.exports = router;   /*暴露这个 router模块*/